const app = getApp()

Page({
  data: {
    userInfo: null,
    options: ['近期热量消耗统计', '自动调温方案管理', '其他选项'],
    extra: ['初始化自动调温方案']
  },

  onLoad: function (options) {
    wx.getUserInfo({
      success: res => {
        this.setData({
          userInfo: res.userInfo
        })
        console.log('welcome~', res.userInfo.nickName)
      }
    })
  },

  authorize: function(e) {
    wx.authorize({
      scope: 'scope.userInfo',
    })
  },

  select: function(e) {
    wx.vibrateShort()

    var index = e.target.dataset.id
    
    if(index == 1) {
      wx.navigateTo({
        url: '/pages/planManage/planManage',
      })
    }
    else if(index == 0) {
      wx.navigateTo({
        url: '/pages/showImage/showImage',
      })
    }
    else if(index == 2) {
      wx.navigateTo({
        url: '/pages/mySettings/mySettings',
      })
    }
    else {
      this.show(this.data.options[index])
    }
  },

  selectExtra: function(e) {
    wx.vibrateShort()

    var index = e.target.dataset.id
    
    if(index == 0) {
      this.clear()
    }
    else {
      this.show(this.data.extra[index])
    }
  },

  show: function(str) {
    wx.showToast({
      title: str,
      icon: 'none'
    })
  },

  clear: function(e) {
    wx.showModal({
      content: '请注意, 该操作无法撤销',
      success: res => {
        if(res.confirm) {
          wx.clearStorage({
            complete: (res) => {},
          })
          wx.showToast({
            title: '初始化成功'
          })
        }
      }
    })
  }
})